package com.key.controller.user;

import com.key.common.R;
import com.key.entity.Assets;
import com.key.entity.AssetsAllocation;
import com.key.entity.SysUser;
import com.key.service.IndexAssetsService;
import com.key.service.IndexService;
import com.key.utils.OddNumbersUtils;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.spring.web.json.Json;

import java.security.Timestamp;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: li
 * @CreateTime: 2023-02-17  15:24
 */
@Controller
@RequestMapping("/userAssets")
@Api(value = "用户资产",tags = "用户资产数据接口")
public class IndexAssetsController {
//    Subject subject = SecurityUtils.getSubject();
//    String s = String.valueOf(subject.getPrincipal());
    @Autowired
    IndexAssetsService indexAssetsService;
    @Autowired
    IndexService indexService;

    /**
     * 查询当前用户所有资产信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/showAllUserAssetsInfo",method = RequestMethod.GET)
    @ApiOperation(value = "查询当前用户所有资产信息",notes = "查询当前用户所有资产信息",httpMethod = "get",response = Json.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页码数",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页显示记录数",paramType = "query",dataType = "int",required = true)
    })

    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showAllUserAssetsInfo(@RequestParam(name = "currentPage") Integer currentPage,
                                   @RequestParam(name = "pageSize") Integer pageSize){

        Subject subject = SecurityUtils.getSubject();
        String s = String.valueOf(subject.getPrincipal());
        SysUser sysUser = indexService.showUserInfoByUserName(s);
        System.out.println(s);
        System.out.println(sysUser);
        Integer assetsManagerId=1;//sysUser.getUserId();
        System.out.println(assetsManagerId);

        R r = indexAssetsService.showUserAssetsInfo(currentPage, pageSize,assetsManagerId);

        return r;
    }



    /**
     * 回显当前资产所属公司
     * @return
     */
    @RequestMapping(value = "/showAssetsInCompany",method = RequestMethod.GET)
    @ApiOperation(value = "查询当前用户公司信息",notes = "查询当前用户公司信息",httpMethod = "get",response = Json.class)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showAssetsInCompany(){
        Subject subject = SecurityUtils.getSubject();
        String s = String.valueOf(subject.getPrincipal());
        SysUser sysUser = indexService.showUserInfoByUserName(s);
        Integer assetsManagerId=1;//sysUser.getUserId();


        R r = indexAssetsService.showAssetsInCompany(assetsManagerId);
        return r;
    }



    /**
     * 显示所有公司名字
     * @return
     */
    @RequestMapping(value = "/showAllCompany",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有公司名字",notes = "查询所有公司名字",httpMethod = "get",response = Json.class)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showAllCompany(){

        R r = indexAssetsService.showAllCompany();

        return r;
    }



    /**
     * 根据公司显示所有部门名字
     * @param companyId
     * @return
     */
    @RequestMapping(value = "/showAllDepartment",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有部门名字",notes = "查询所有部门名字",httpMethod = "get",response = Json.class)
    @ResponseBody
    @ApiImplicitParam(name = "companyId",value = "选中的公司id",required = true,dataType = "int",paramType ="query")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showAllDepartment(@RequestParam(name = "companyId") Integer companyId){

        R r = indexAssetsService.showAllDepartmentByCompanyId(companyId);

        return r;
    }

    //根据部门显示所有员工名字
    @RequestMapping(value = "/showAllPerson",method = RequestMethod.GET)
    @ApiOperation(value = "查询部门负责人名字",notes = "查询部门负责人名字",httpMethod = "get",response = Json.class)
    @ResponseBody
    @ApiImplicitParam(name = "departmentId",value = "选中的部门id",required = true,dataType = "int",paramType ="query")

    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showAllPerson(@RequestParam(name = "departmentId") Integer departmentId){

        R r = indexAssetsService.showPersonByDeptId(departmentId);

        return r;
    }

    /**
     * 根据用户显示可操作资产
     * @return
     */
    @RequestMapping(value = "/showAllOperationalAssets",method = RequestMethod.GET)
    @ApiOperation(value = "查询用户可操作资产",notes = "查询用户可操作资产",httpMethod = "get",response = Json.class)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showAllOperationalAssets(){
        Subject subject = SecurityUtils.getSubject();
        String s = String.valueOf(subject.getPrincipal());

        SysUser sysUser = indexService.showUserInfoByUserName(s);
        Integer assetsManagerId=1;//sysUser.getUserId();
        R r = indexAssetsService.showUserOperationalAssets(assetsManagerId);
        System.out.println(r);
        return r;
    }





    //添加资产调拨信息单
    @RequestMapping(value = "/addAssetsApproval",method = RequestMethod.POST)
    @ApiOperation(value = "添加资产调拨单信息",notes = "添加资产调拨单信息",httpMethod = "post",response = Json.class)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "outDate",value = "调出日期",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "companyOutName",value = "调出公司",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "companyInName",value = "调入公司",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "assetsIds",value = "选中的资产id,可多个",paramType = "query",dataType = "int[]",required = true,allowMultiple = true),
            @ApiImplicitParam(name = "inPerson",value = "转移用户",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "description",value = "调拨说明",paramType = "query",dataType = "String",required = true)

    })

    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R addAssetsApproval( @RequestParam(name = "outDate") String outDate,
                                @RequestParam(name = "companyOutName") String outCompany,
                                @RequestParam(name = "companyInName") String inCompany,
                                @RequestParam(name = "description") String description,
                                @RequestParam(name = "assetsIds") Integer[] assetsIds,
                                @RequestParam(name = "inPerson") String inPerson
                               ){

        System.out.println(assetsIds);
        Subject subject = SecurityUtils.getSubject();
        String s = String.valueOf(subject.getPrincipal());

        /*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(outDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
*/
        AssetsAllocation assetsAllocation = new AssetsAllocation();
        assetsAllocation.setAllocationNum(OddNumbersUtils.creatON("ZY"));
        assetsAllocation.setAllocationState("未完成");
        assetsAllocation.setOutDate(outDate);
        assetsAllocation.setOutCompany(outCompany);
        assetsAllocation.setInCompany(inCompany);
        assetsAllocation.setInPerson(inPerson);
        //后端获取当前用户
        SysUser sysUser = indexService.showUserInfoByUserName(s);
      //  String userName = sysUser.getUserName();
        String outPerson="张三";
        assetsAllocation.setOutPerson(outPerson);
        assetsAllocation.setDescription(description);

        R r = indexAssetsService.addAssetsAllocation(assetsAllocation, assetsIds);

        return r;
    }







}
